package com.tarry.service.impl;

import com.google.common.base.Preconditions;
import com.tarry.bean.PageQuery;
import com.tarry.bean.PageResult;
import com.tarry.common.RequestHolder;
import com.tarry.dao.SysAclMapper;
import com.tarry.exception.ParamException;
import com.tarry.model.entity.SysAcl;
import com.tarry.model.vo.AclVo;
import com.tarry.service.SysAclService;
import com.tarry.util.BeanValidator;
import com.tarry.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SysAclServiceImpl extends BaseService<SysAcl> implements SysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Override
    public SysAcl findByKeyword(String keyword) {
        return null;
    }

    @Override
    public void save(AclVo entity) {
        BeanValidator.check(entity);
        if (checkExist(entity.getAclModuleId(),
                entity.getName(),
                entity.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限点");
        }
        SysAcl acl = SysAcl.builder()
                .name(entity.getName())
                .aclModuleId(entity.getAclModuleId())
                .url(entity.getUrl())
                .type(entity.getType())
                .status(entity.getStatus())
                .seq(entity.getSeq())
                .remark(entity.getRemark()).build();
        acl.setCode(generateCode());
        setOpratorInfo(acl);
        sysAclMapper.insertSelective(acl);
        //sysLogService.saveAclLog(null, acl);
    }

    private boolean checkExist(int aclModuleId, String name, Integer id) {
        return sysAclMapper.countByNameAndAclModuleId(aclModuleId, name, id) > 0;
    }

    private String generateCode() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date()) + "_" + (int)(Math.random() * 100);
    }

    @Override
    public void update(AclVo entity) {
        BeanValidator.check(entity);
        if (checkExist(entity.getAclModuleId(),
                entity.getName(),
                entity.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限点");
        }
        SysAcl before = sysAclMapper.selectByPrimaryKey(entity.getId());
        Preconditions.checkNotNull(before, "待更新的权限点不存在");

        SysAcl after = SysAcl.builder()
                .id(entity.getId())
                .name(entity.getName())
                .aclModuleId(entity.getAclModuleId())
                .url(entity.getUrl())
                .type(entity.getType())
                .status(entity.getStatus())
                .seq(entity.getSeq())
                .remark(entity.getRemark())
                .build();
        setOpratorInfo(after);
        sysAclMapper.updateByPrimaryKeySelective(after);
        //sysLogService.saveAclLog(before, after);

    }

    @Override
    public void delete(int id) {

    }

    public PageResult<SysAcl> getPageByAclModuleId(int aclModuleId, PageQuery page) {
        BeanValidator.check(page);
        int count = sysAclMapper.countByAclModuleId(aclModuleId);
        if (count > 0) {
            List<SysAcl> aclList = sysAclMapper.getPageByAclModuleId(aclModuleId, page);
            return PageResult.<SysAcl>builder().data(aclList).total(count).build();
        }
        return PageResult.<SysAcl>builder().build();
    }
}
